// pages/center/index.js
var api = require('../../utils/api.js')
var app = new getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH: 0,
    isIphoneX: false,
    show:false,
    userInfo: null,
    navData:[{
      src:"/imgs/center/collect.png",
      text:"我的收藏"
    },{
      src:"/imgs/center/clock.png",
      text:"我的足迹"
    },{
      src:"/imgs/center/tip.png",
      text:"我的消息"
    }],
    infoData:[{
      src:"/imgs/center/banner.png",
      text:"品牌介绍"
    },{
      src:"/imgs/center/new.png",
      text:"新闻动态"
    },{
      src:"/imgs/center/offline.png",
      text:"线下门店"
    },{
      src:"/imgs/center/case.png",
      text:"工程案例"
    }],
    content:null,
    form:{
      real_name:'',
      r_phone:'',
      message:''
    },
    area:'',
    p_id:'',
    c_id:'',
    a_id:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //data 
    let isIphoneX = app.globalData.isIphoneX
    if (isIphoneX) {
      this.setData({
        isIphoneX
      })
    }
    this.setData({
      navH: app.globalData.navHeight
    })
  },
  showPopup(){
    this.setData({
      show:true
    })
  },
  close(e){
    this.setData({
      show:false,
      area:e.detail.value.join(","),
      p_id:e.detail.p_id,
      c_id:e.detail.c_id,
      a_id:e.detail.a_id
    })
  },
  getData(){
    let params={
      member_id:api.cache('user').member_id||"",
    }
    api.post('my_center_info',params).then(res => {
      if (res.code == 200) {
        this.setData({
          content:res.data
        })
      }
    })
  },
  toLogin() {
    wx.navigateTo({
      url: '/pages/grant/grant',
    })
  },
  goto(e){
    let res = api.checkLogin()
    if (!res) {
      return;
    }
    let index = e.currentTarget.dataset.index
    let src = ''
    if(index == 0){
      src = '/pages/center/collection/collection'
    }else if(index == 1){
      src = '/pages/center/footprint/footprint'
    }else if(index == 2){
      src = '/pages/center/message/message'
    }else if(index == 3){
      return;
    }
    wx.navigateTo({
      url: src,
    })
  },
  toLogin() {
    wx.navigateTo({
      url: '/pages/grant/grant',
    })
  },
  takePhone(){
    if(this.data.userInfo.member.phone){
      return
    }
    let _that = this;
    wx.navigateTo({
      url: '/pages/grant/grant',
    })
    return;
    wx.makePhoneCall({
      phoneNumber: _that.data.userInfo.member,
    })
  },
  toDetail(e) {
    let item = e.currentTarget.dataset.item
    if(item.is_soft==1){
      wx.navigateTo({
        url: `/pages/goods/detail/detail?id=${item.id}&type=1`,
      })
    }else{
      wx.navigateTo({
        url: '/pages/goods/detail/detail?id=' + item.id,
      })
    }
    // wx.navigateTo({
    //   url: '/pages/goods/detail/detail?id=' + id,
    // })
  },
  gotoInfo(e){
    const index = e.currentTarget.dataset.index
    let url = ''
    if(index == 0){//品牌介绍
      url = '/pages/index/brand/brand';
    }else if(index == 1){//新闻动态
      url = '/pages/look/index';
    }else if(index == 2){//线下门店
      url = '/pages/index/shops/index';
    }else if(index == 3){//工程案例
      url = '/pages/index/case/case';
    }
    wx.navigateTo({
      url
    })
  },
  submit(e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    if(!e.detail.value.real_name){
      api.alert("请输入姓名")
      return;
    }
    if(!e.detail.value.r_phone){
      api.alert("请输入手机号码")
      return;
    }
    if(!this.data.area){
      api.alert("请选择地区")
      return;
    }
    if(!e.detail.value.message){
      api.alert("请输入您的需求")
      return;
    }
    let params = {
      ...e.detail.value,
      p_id:this.data.p_id,
      c_id:this.data.c_id,
      a_id:this.data.a_id,
      member_id:api.cache('user').member_id||""
    }
    api.post('submit_message',params).then(res => {
      if (res.code == 200) {
        api.tip("提交成功")
        let form = this.data.form
        form.real_name = ''
        form.r_phone = ''
        form.message = ''
        this.setData({
          form,
          area:'',
          p_id: '',
          c_id: '',
          a_id: '',
        })
      }
    }).catch((err)=>{
      api.tip(err.data.msg)
    })
  },
  toAppointment(){
    wx.navigateTo({
      url: '/pages/appointment/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData()
    //判断是否登录
    let user = api.cache("user")
    if (user.nickname && user.headimgurl) {
      this.setData({
        userInfo: user
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      // imageUrl: '../../imgs/index/share.jpg',
      // title: app.globalData.shareTitle,
      // path: '/pages/index/index'
    }
  }
})